﻿using System;
using System.Text;
using System.Collections.Generic;
using System.IO;

namespace Movies
{
    public class HelperSettingProvider
    {
        private static String rootReadLocation;
        private List<String> settingKey;
        private List<String> settingValue;
        private static HelperSettingProvider settingProvider;
        private HelperSettingProvider()
        {
            settingKey = new List<String>();
            settingValue = new List<String>();
        }

        public static void SettingLocation(String readLocation)
        {
            rootReadLocation = readLocation; ;
        }

        public static HelperSettingProvider Default()
        {
            if (settingProvider == null)
            {
                settingProvider = new HelperSettingProvider();
            }

            return settingProvider;
        }

        private void Load()
        {
            try
            {
                String[] fileNames = Directory.GetFiles(rootReadLocation);
                settingKey = new List<String>();
                settingValue = new List<String>();

                foreach (String fileName in fileNames)
                {
                    byte[] buffer;
                    String value = "";
                    FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    try
                    {
                        int length = (int)fileStream.Length;  // get file length
                        buffer = new byte[length];            // create buffer
                        int count;                            // actual number of bytes read
                        int sum = 0;                          // total number of bytes read

                        // read until Read method returns 0 (end of the stream has been reached)
                        while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                        {
                            sum += count;  // sum is a buffer offset for next reading
                        }

                        value = System.Text.Encoding.GetEncoding("utf-8").GetString(buffer);

                    }
                    catch (Exception ex)
                    {
                        HelperContentProvider.OmitWarning(ex);
                    }
                    settingKey.Add(fileName.Substring(fileName.LastIndexOf('\\') + 1));
                    settingValue.Add(value);

                }
            }catch(Exception eax){
                        HelperContentProvider.OmitWarning(eax);
                
            }
        }


        public String Get(String key)
        {
            int index = settingKey.IndexOf(key);
            if (index > 0)
            {
                return getValue(index);
            }
            else
            {
                return "";
            }
        }

        public void Set(String key, String value)
        {
            int index = settingKey.IndexOf(key);
            if (index > 0)
            {
                settingKey.RemoveAt(index);
                settingValue.RemoveAt(index);
            }
            settingKey.Add(key);
            settingValue.Add(value);
        }


        public void Save()
        {
            foreach (String fileName in settingKey)
            {
                try
                {
                File.WriteAllText(rootReadLocation + "\\" + fileName, Get(fileName));
                }
                catch (Exception ex)
                {
                   HelperContentProvider.OmitWarning(ex);
                }
            }
            Load();
        }


        private String getValue(int lookupIndex)
        {

            int index = -1;
            if (lookupIndex > 0)
            {
                foreach (String keyVal in settingValue)
                {
                    index++;
                    if (index == lookupIndex)
                    {
                        return keyVal;
                    }
                }
            }
            return null;
        }



        public String ButtonSize { get { return Get("ButtonSize"); } set { Set("ButtonSize", value); } }
        public String DataStoreLocation { get { return Get("DataStoreLocation"); } set { Set("DataStoreLocation", value); } }
        public String DefaultDisplay { get { return Get("DefaultDisplay"); } set { Set("DefaultDisplay", value); } }
        public String ExtList { get { return Get("ExtList"); } set { Set("ExtList", value); } }
        public String FileActionLastSelectedPath { get { return Get("FileActionLastSelectedPath"); } set { Set("FileActionLastSelectedPath", value); } }

        public String FirstName { get { return Get("FirstName"); } set { Set("FirstName", value); } }
        public String LastName { get { return Get("LastName"); } set { Set("LastName", value); } }
        public String NickName { get { return Get("NickName"); } set { Set("NickName", value); } }

        public String FolderNames { get { return Get("FolderNames"); } set { Set("FolderNames", value); } }
        public String IsSynchronized { get { return Get("IsSynchronized"); } set { Set("IsSynchronized", value); } }
        public String MovieDataStoreLocation { get { return Get("MovieDataStoreLocation"); } set { Set("MovieDataStoreLocation", value); } }
        public String NameChangeInterest { get { return Get("NameChangeInterest"); } set { Set("NameChangeInterest", value); } }

        public String DefaultStore { get { return Get("DefaultStore"); } set { Set("DefaultStore", value); } }
        public String OldDataStoreLocation { get { return Get("OldDataStoreLocation"); } set { Set("OldDataStoreLocation", value); } }
        public String QuickStartShown { get { return Get("QuickStartShown"); } set { Set("QuickStartShown", value); } }
        public String TmdbAPIKey { get { return Get("TmdbAPIKey"); } set { Set("TmdbAPIKey", value); } }
        

    }
}
